<template>
	<view class="login">
		<nav-bar centerTitle='登录' :leftIcon='true'></nav-bar>
		<image src="@/static/login/loginBg.png" class="login-bg"></image>
		<view class="info-block">
			<view class="login-block">
				<image src="../../static/login/logo.png" class="logo"></image>
				<view class="txt">商家登录</view>
			</view>
			<view class="login-submit-option">
				<view class="item" style="margin-top: 34rpx;">
					<view class="left-option">手机账号</view>
					<view class="right-option">
						<input class="input-option" v-model="loginParam.mobile" placeholder="请输入您的手机账号" />
						<uni-icons type="right" size="24"></uni-icons>
					</view>
				</view>
				<view class="item" style="margin-top: 34rpx;">
					<view class="left-option">登录密码</view>
					<view class="right-option">
						<input class="input-option" type="password" v-model="loginParam.pwd" placeholder="请输入您的登录密码" />
					</view>
				</view>
			</view>
		</view>
		<view class="btn submit" @click="submitClick">
			登录
		</view>
		<uv-code :seconds="seconds" ref="code" @change="codeChange"></uv-code>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tips: '',
				seconds: 60,
				loginParam: {
					mobile: '',
					pwd: '',
				}
			}
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (!this.loginParam.mobile) {
					return this.$tools.showMsg('您的手机号不能为空')
				}
				if (this.loginParam.mobile.length < 11) {
					return this.$tools.showMsg('请您填写正确的手机号')
				}
				if (this.$refs.code.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					this.$http('common.sendCode', {
						mobile: this.loginParam.mobile
					}).then(res => {
						uni.hideLoading();
						if (res.code == 200) {
							this.$tools.showMsg('验证码已发送')
							this.$refs.code.start();
						} else {
							this.$tools.showMsg(res.msg)
						}
					})
				} else {
					this.$tools.showMsg('验证码倒计时中')
				}
			},
			 
			submitClick() {
				if (!this.loginParam.mobile) {
					return this.$tools.showMsg('您的手机号不能为空')
				}
				if (this.loginParam.mobile.length < 11) {
					return this.$tools.showMsg('请您填写正确的手机号')
				}
				if (!this.loginParam.pwd) {
					return this.$tools.showMsg('您的密码不能为空')
				}
				this.$http('common.login',this.loginParam).then(res => {
					if (res.status == 0) {
						uni.setStorageSync('token', res.data.uuid)
						uni.setStorageSync('adminId', res.data.adminId)
						this.$tools.showMsg('登录成功')
						this.getUserInfo()
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/index/index'
							})
						}, 600)
					} else {
						this.$tools.showMsg(res.msg)
					}
				})
			},
			getUserInfo() {
				let adminId=uni.getStorageSync('adminId')
				let token=uni.getStorageSync('token')
				uni.request({
					url: 'https://api.shaoyanghmk.cn/admin/' + adminId, //仅为示例，并非真实接口地址。
					header: {
						'Token': token //自定义请求头信息
					},
					success: (res) => {
						console.log(res.data);
						let data = res.data
					}
				}); 		  
			}

		}
	}
</script>

<style lang="scss">
	.login {
		height: 100vh;
	}

	.login-bg {
		height: 100vh;
		width: 100%;
		position: relative;

	}

	.avatar-wrapper {
		padding: 0;
		width: 68rpx !important;
		height: 68rpx !important;
		background: none !important;
		margin-right: 32rpx;

	}

	button::after {
		border: none;
	}

	.info-block {
		position: absolute;
		z-index: 888;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
	}

	.login-block {

		margin-top: 400rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;

		.logo {
			width: 180rpx;
			height: 180rpx;
		}

		.txt {
			margin-top: 40rpx;
			font-size: 36rpx;
			font-weight: 400;
			color: #0089CF;
		}
	}

	.login-submit-option {
		margin-top: 150rpx;
		margin-left: 48rpx;
		margin-right: 48rpx;

		.item {
			display: flex;
			align-items: center;
			padding: 28rpx 0;
			border-bottom: 1rpx solid #EEEEEE;

			.left-option {
				flex: none;
				color: #181818;
				font-size: 30rpx;
				width: 150rpx;
			}

			.right-option {
				flex: auto;
				display: flex;
				align-items: center;
				// justify-content: flex-end;

				.img {
					width: 68rpx;
					height: 68rpx;

					border-radius: 50%;
				}

				.input-option {
					// width: 100%;
					margin-right: 32rpx;

					flex: auto;
				}

				.code-txt {
					flex: none;
					height: 45rpx;
					padding: 12rpx 24rpx;
					font-size: 24rpx;
					line-height: 45rpx;
					text-align: center;
					color: #fff;
				}
			}
		}
	}


	.submit {
		position: fixed;
		bottom: 74rpx;
		left: 24rpx;
		right: 24rpx;
		margin: 0 24rpx;
		z-index: 999;
		height: 90rpx;
		font-size: 30rpx;
		line-height: 90rpx;
		text-align: center;
		color: #fff;
	}
</style>